package cn.iocoder.yudao.module.chatgpt.controller.admin.chat;


import cn.iocoder.yudao.module.chatgpt.anno.ChatAuth;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.params.chat.ChatMessage;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.params.chat.ChatParams;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.params.chat.ChatResult;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.params.edit.EditParams;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.params.edit.EditResult;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.params.edit.constant.EditModelEnum;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.params.image.ImageParams;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.params.image.ImageResult;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.vo.ChatDTO;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.vo.EditDTO;
import cn.iocoder.yudao.module.chatgpt.controller.admin.chat.vo.ImageDTO;
import cn.iocoder.yudao.module.chatgpt.service.chat.ChatCacheService;
import cn.iocoder.yudao.module.chatgpt.service.chat.ChatService;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author kindear
 * chat 聊天相关接口
 */
@Tag(name = "管理后台 - OpenAI服务")
@RestController
@RequestMapping("/chatgpt/openai")
@Validated
@Slf4j
public class ChatController {
    @Resource
    ChatService chatService;

    @Resource
    ChatCacheService cacheService;

    @ChatAuth
    @PostMapping("chat")
    @Operation(summary = "聊天")
    public Object doChat(@RequestBody ChatDTO dto){
        ChatParams params = new ChatParams();

        List<ChatMessage> messages = cacheService.history(dto.getChatId(), dto.getWithContext());
        // 构建用户信息
        ChatMessage userMsg = chatService.buildUserMessage(dto.getContent());
        messages.add(userMsg);
        params.setMessages(messages);
        // 写入缓存服务
        cacheService.write(dto.getChatId(), userMsg);
        // 执行
        ChatResult result = chatService.doChat(params, dto.getChatId());

        return chatService.simpleResult(result);
    }

    @ChatAuth
    @PostMapping("doAICreation")
    @Operation(summary = "AI伪创作")
    public Object doAICreation(@RequestBody ChatDTO dto){
        ChatParams params = new ChatParams();

        List<ChatMessage> messages = cacheService.history(dto.getChatId(), dto.getWithContext());
        // 构建用户信息
        ChatMessage userMsg = null;
        if (messages != null && messages.size() > 0) {
            userMsg = chatService.buildUserMessage(dto.getContent());
        } else {
            userMsg = chatService.buildUserMessage(dto);
        }
        messages.add(userMsg);
        params.setMessages(messages);
        // 写入缓存服务
        cacheService.write(dto.getChatId(), userMsg);
        // 执行
        ChatResult result = chatService.doChat(params, dto.getChatId());

        return chatService.simpleResult(result);
    }

    @ChatAuth
    @Hidden
    @PostMapping("chat/str")
    @Operation(summary = "聊天STR")
    public Object doChatStr(@RequestBody ChatDTO dto){
        ChatParams params = new ChatParams();

        List<ChatMessage> messages = chatService.getContext(dto.getChatId(),dto.getWithContext());
        messages.add(chatService.buildUserMessage(dto.getContent()));
        params.setMessages(messages);
        // 执行
        return chatService.doChatStr(params, dto.getChatId());
    }

    @ChatAuth
    @PostMapping("edit/text")
    @Operation(summary = "文本编辑")
    public Object doEditText(@Validated @RequestBody EditDTO dto){
        EditParams params = new EditParams();
        params.setInput(dto.getContent());
        params.setInstruction(dto.getTips());
        params.setModel(EditModelEnum.TEXT.getModel());
        EditResult result = chatService.doEdit(params);
        return chatService.simpleResult(result);
    }

    @ChatAuth
    @PostMapping("edit/code")
    @Operation(summary = "代码编辑")
    public Object doEditCode(@Validated @RequestBody EditDTO dto){
        EditParams params = new EditParams();
        params.setInput(dto.getContent());
        params.setInstruction(dto.getTips());
        params.setModel(EditModelEnum.CODE.getModel());
        EditResult result = chatService.doEdit(params);
        return chatService.simpleResult(result);
    }

    @ChatAuth
    @PostMapping("draw")
    @Operation(summary = "图像绘制")
    public Object doDraw(@Validated @RequestBody ImageDTO dto){
        ImageParams params = new ImageParams();
        // 设置描述
        params.setPrompt(dto.getTips());
        // 设置返回结果个数
        params.setN(dto.getN());
        ImageResult result = chatService.doDraw(params);
        return chatService.simpleResult(result);
    }

    @ChatAuth
    @GetMapping("chat/{chatId}")
    @Operation(summary = "聊天记录")
    public Object chatHistory(@PathVariable("chatId") String chatId,@RequestParam(value = "limit",defaultValue = "10") Integer limit){
        return cacheService.history(chatId,limit);
    }

}
